package vn.hust.homebudget.core.broadcastreceiver;

import vn.hust.homebudget.BudgetBaseActivity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public abstract class BudgetBroadcastReceiver extends BroadcastReceiver implements BudgetBroadcastReceiverConstant {
	private String action;
	private Bundle bundle;
	private IntentFilter appIntentFilter = new IntentFilter();
	
	public BudgetBroadcastReceiver() {
		super();
		addActionIntentFilter();
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		setAction(intent.getAction());
		setBundle(intent.getExtras());
		handleReceiver(context, intent);
	}
	public abstract void addActionIntentFilter();
	public abstract void handleReceiver(Context context, Intent intent);
	/** get Bundle Message*/
	public String getMessageBundle() {
		if(getBundle()!=null){
			return getBundle().getString(BudgetBroadcastReceiverConstant.BROADCAST_BUNDLE_MSG);
		}
		return null;
	}	
	/** get Bundle ClassName*/
	public String getClassNameBundle() {
		if(getBundle()!=null){
			return getBundle().getString(BudgetBroadcastReceiverConstant.BROADCAST_BUNDLE_CLASSNAME);
		}
		return null;
	}	
	/** compare Broadcast action to the broadcast filter*/
	public boolean compareAction(String broadcastAction) {
		if (broadcastAction.equalsIgnoreCase(getAction())){
			return true;
		}
		return false;
	}
	/** compare Broadcast action with CLASSNAME bundle to the broadcast filter*/
	public boolean compareAction(String broadcastAction,BudgetBaseActivity activity) {
		if (compareAction(broadcastAction) == true && compareClassName(activity) == true){
			return true;
		}
		return false;
	}
	/** compare ClassName bundle to the broadcast filter*/
	public boolean compareClassName(BudgetBaseActivity activity) {
		if (getBundle()!=null){
			String ClassName = getClassNameBundle();
			if (ClassName != null && ClassName.equalsIgnoreCase(activity.getClass().getName())){
				return true;
			}
		}
		return false;
	}
	/** handle basic exception when receive broadcast handle error exception*/
	public void handleException(String msg,final BudgetBaseActivity activity){
		new AlertDialog.Builder(activity.getApplicationContext()).setMessage(msg).
		setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
					int which) {
				dialog.dismiss();
				activity.finish();
			}
		}).setNegativeButton("QUIT", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				sendBroadcastQuitMessage(activity);
			}
		}).setCancelable(false).show();
	}
	
	/** send broadcast QUIT message to quit the application*/
	public void sendBroadcastQuitMessage(BudgetBaseActivity activity) {
		Intent intent = new Intent(BROADCAST_ACTION_QUIT);
		activity.sendBroadcast(intent);
	}
	/**
	 * @return the action
	 */
	public String getAction() {
		return action;
	}
	/**
	 * @param action the action to set
	 */
	public void setAction(String action) {
		this.action = action;
	}
	/**
	 * @return the bundle
	 */
	public Bundle getBundle() {
		return bundle;
	}
	/**
	 * @param bundle the bundle to set
	 */
	public void setBundle(Bundle bundle) {
		this.bundle = bundle;
	}
	/**
	 * @return the appIntentFilter
	 */
	public IntentFilter getAppIntentFilter() {
		return appIntentFilter;
	}
	/**
	 * @param appIntentFilter the appIntentFilter to set
	 */
	public void setAppIntentFilter(IntentFilter appIntentFilter) {
		this.appIntentFilter = appIntentFilter;
	}
	
}
